﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using T.ApiResults;
using T.Settlement.API.Application.Command.InvoiceCommand;

namespace T.Settlement.API.Controllers
{
    /// <summary>
    /// 进/销项控制器(写)
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class InvoiceController : ControllerBase
    {
        private readonly IMediator _mediator;
        public InvoiceController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加进/销项
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Create(InvoiceCreateCommand command)
        {
            ApiResult<int> result= new ApiResult<int>();
            try
            {
                result.Data=await _mediator.Send(new InvoiceCreateCommand
                {
                    InvoiceId = command.InvoiceId,
                    InvoiceNumber = command.InvoiceNumber,
                    InvoicingUnit = command.InvoicingUnit,
                    CustomerName = command.CustomerName,
                    InvoiceType = command.InvoiceType,
                    InvoiceValue = command.InvoiceValue,
                    TaxRate = command.TaxRate,
                    InvoicingDate = command.InvoicingDate,
                    Notes = command.Notes,
                    InvoiceCreator = command.InvoiceCreator,
                    InvoiceClassify = command.InvoiceClassify,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取用户名称
                    CreatedDate = DateTime.Now,
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除进/销项
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Delete(InvoiceDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new InvoiceDelCommand
                {
                    InvoiceId = command.InvoiceId,
                    InvoiceNumber = command.InvoiceNumber,
                    InvoicingUnit = command.InvoicingUnit,
                    CustomerName = command.CustomerName,
                    InvoiceType = command.InvoiceType,
                    InvoiceValue = command.InvoiceValue,
                    TaxRate = command.TaxRate,
                    InvoicingDate = command.InvoicingDate,
                    Notes = command.Notes,
                    InvoiceCreator = command.InvoiceCreator,
                    InvoiceClassify = command.InvoiceClassify,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取用户名称
                    DeletedDate=DateTime.Now
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改进/销项
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Update(InvoiceUpdCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new InvoiceUpdCommand
                {
                    InvoiceId = command.InvoiceId,
                    InvoiceNumber = command.InvoiceNumber,
                    InvoicingUnit = command.InvoicingUnit,
                    CustomerName = command.CustomerName,
                    InvoiceType = command.InvoiceType,
                    InvoiceValue = command.InvoiceValue,
                    TaxRate = command.TaxRate,
                    InvoicingDate = command.InvoicingDate,
                    Notes = command.Notes,
                    InvoiceCreator = command.InvoiceCreator,
                    InvoiceClassify = command.InvoiceClassify,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取用户名称
                    UpdatedDate=DateTime.Now
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 批删进/销项
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> BatchDel([FromQuery]InvoiceBatchDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
